//
// Created by imagination on 4/8/25.
//
#include "setting_datetime_page.h"

#include <stdio.h>

#include "application.h"

#include "lv_i18n/lv_i18n.h"

static int32_t col_desc[] = { LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST };
static int32_t row_desc[] = { LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST };
static int32_t col_desc2[] = { LV_GRID_FR(2), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST };
static int32_t row_desc2[] = { LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST };
static uint8_t days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static lv_obj_t * year_roller;
static lv_obj_t * month_roller;
static lv_obj_t * day_roller;

static void year_changed_cb(lv_event_t * e) {

}

static void month_changed_cb(lv_event_t * e) {

}

static void day_changed_cb(lv_event_t * e) {

}

static void hour_changed_cb(lv_event_t * e) {

}

static void minute_changed_cb(lv_event_t * e) {

}

static void time_format_changed_cb(lv_event_t * e) {
    lv_obj_t * obj = lv_event_get_target(e);
    uint8_t index = lv_dropdown_get_selected(obj);
    if (app.settings.time_format != index) {
        app.settings.time_format = index;
    }
}

static void fragment_constructor_ctor(lv_fragment_t * self, void * args) {

}

static lv_obj_t * fragment_create_obj(lv_fragment_t * fragment, lv_obj_t * container) {
    lv_obj_t * root = lv_obj_create(container);
    lv_obj_set_size(root, LV_PCT(100), LV_PCT(100));
    lv_obj_set_layout(root, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(root, LV_FLEX_FLOW_COLUMN);

    char str[512];
    lv_obj_t * label = NULL;

    lv_obj_t * top_container = lv_obj_create(root);
    lv_obj_set_layout(top_container, LV_LAYOUT_GRID);
    lv_obj_set_grid_dsc_array(top_container, col_desc, row_desc);
    lv_obj_set_size(top_container, LV_PCT(100), LV_SIZE_CONTENT);

    memset(str, '\0', sizeof(str));
    for (uint16_t i = 2025; i < 2079; i++) {
        sprintf(&str[strlen(str)], "%d\n", i);
    }
    str[strlen(str)-1] = '\0';
    year_roller = lv_roller_create(top_container);
    lv_roller_set_visible_row_count(year_roller, 5);
    lv_roller_set_options(year_roller, str, LV_ROLLER_MODE_INFINITE);
    label = lv_label_create(top_container);
    lv_label_set_text_static(label, _("Year"));
    lv_obj_set_grid_cell(year_roller, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_add_event_cb(year_roller, year_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    month_roller = lv_roller_create(top_container);
    lv_roller_set_visible_row_count(month_roller, 5);
    lv_roller_set_options(month_roller, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12", LV_ROLLER_MODE_INFINITE);
    label = lv_label_create(top_container);
    lv_label_set_text_static(label, _("Month"));
    lv_obj_set_grid_cell(month_roller, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_add_event_cb(month_roller, month_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    memset(str, '\0', sizeof(str));
    uint16_t year = lv_roller_get_selected(year_roller) + 2025;
    uint16_t month = lv_roller_get_selected(month_roller) + 1;
    for (uint8_t i = 1; i <= (uint8_t)((month == 2) ? (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 29 : 28) : days[month-1]); i++) {
        sprintf(&str[strlen(str)], "%d\n", i);
    }
    str[strlen(str)-1] = '\0';
    day_roller = lv_roller_create(top_container);
    lv_roller_set_visible_row_count(day_roller, 5);
    lv_roller_set_options(day_roller, str, LV_ROLLER_MODE_INFINITE);
    label = lv_label_create(top_container);
    lv_label_set_text_static(label, _("Day"));
    lv_obj_set_grid_cell(day_roller, LV_GRID_ALIGN_STRETCH, 2, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, 2, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_add_event_cb(day_roller, day_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    memset(str, '\0', sizeof(str));
    for (uint8_t i=0; i < 24; i++) {
        sprintf(&str[strlen(str)], "%d\n", i);
    }
    str[strlen(str)-1] = '\0';
    lv_obj_t * hour_roller = lv_roller_create(top_container);
    lv_roller_set_visible_row_count(hour_roller, 5);
    lv_roller_set_options(hour_roller, str, LV_ROLLER_MODE_INFINITE);
    label = lv_label_create(top_container);
    lv_label_set_text_static(label, _("Hour"));
    lv_obj_set_grid_cell(hour_roller, LV_GRID_ALIGN_STRETCH, 3, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, 3, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_add_event_cb(hour_roller, hour_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    memset(str, '\0', sizeof(str));
    for (uint8_t i = 0; i < 60; i++) {
        sprintf(&str[strlen(str)], "%d\n", i);
    }
    str[strlen(str)-1] = '\0';
    lv_obj_t * minute_roller = lv_roller_create(top_container);
    lv_roller_set_visible_row_count(minute_roller, 5);
    lv_roller_set_options(minute_roller, str, LV_ROLLER_MODE_INFINITE);
    label = lv_label_create(top_container);
    lv_label_set_text_static(label, _("Minute"));
    lv_obj_set_grid_cell(minute_roller, LV_GRID_ALIGN_STRETCH, 4, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, 4, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_add_event_cb(minute_roller, minute_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);


    lv_obj_t * bottom_container = lv_obj_create(root);
    lv_obj_set_layout(bottom_container, LV_LAYOUT_GRID);
    lv_obj_set_grid_dsc_array(bottom_container, col_desc2, row_desc2);
    lv_obj_set_size(bottom_container, LV_PCT(100), LV_SIZE_CONTENT);

    label = lv_label_create(bottom_container);
    lv_label_set_text_static(label, _("Time format"));
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_t * time_fmt_dropdown = lv_dropdown_create(bottom_container);
    lv_dropdown_clear_options(time_fmt_dropdown);
    lv_dropdown_set_options(time_fmt_dropdown, "12h\n24h");
    lv_dropdown_set_selected(time_fmt_dropdown, app.settings.time_format);
    lv_obj_add_event_cb(time_fmt_dropdown, time_format_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_set_grid_cell(time_fmt_dropdown, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);

    return root;
}

lv_fragment_class_t setting_datetime_fragment_cls = {
    .constructor_cb = fragment_constructor_ctor,
    .create_obj_cb = fragment_create_obj,
    .instance_size = sizeof(lv_fragment_t)
};